package 计时器;

import com.sun.deploy.net.proxy.ProxyUnavailableException;

import java.util.PriorityQueue;
import java.util.Random;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: lenovo
 * Date: 2022-09-26
 * Time: 15:59
 */
//这是一个任务
class MyTask implements Comparable<MyTask>{
    //线程任务
    private Runnable runnable;
    //任务什么时候执行,这里再构造方法里转为时间戳
    private long time;
    public MyTask(Runnable runnable,long delay){
        this.runnable = runnable;
        this.time = System.currentTimeMillis()+delay;
    }

    public Runnable getRunnable() {
        return runnable;
    }

    public long getTime() {
        return time;
    }

    @Override
    public int compareTo(MyTask o) {
        return (int)(this.time-o.time);
    }
}
class MyTimer{
    //线程安全的优先级队列
    BlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();
    Object locker = new Object();
public MyTimer(){
    Thread t = new Thread(()->{
        while (true){
            synchronized (locker){
                MyTask task = null;
                try {
                    task = queue.take();
                long curtime = System.currentTimeMillis();
                if(curtime>= task.getTime()){
                    task.getRunnable().run();
                }else {

                        queue.put(task);
                        locker.wait( task.getTime()-curtime);
                    }
                    }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    });
    t.start();
}


/*class MyTimer{
    //线程安全的优先级队列
    BlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();
    Object locker = new Object();
    public MyTimer(){
        //创建一个线程来获取首队列的元素
        Thread t = new Thread(()->{
            while (true) {
                try {
                    synchronized (locker) {
                        MyTask task = queue.take();
                        long curtime = System.currentTimeMillis();
                        //当前时间达到了或者超过了都要执行任务
                        if (curtime >= task.getTime()) {
                            task.getRunnable().run();
                        } else {//时间没达到,放回队列里
                            queue.put(task);

                            //等待多久:任务时间-当前时间
                            locker.wait(task.getTime() - curtime);
                            //如果wait被notify唤醒了,while循环又会重新检查队首元素,看
                        }
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        //线程开始执行
        t.start();
    }

*/


  /*  class MyTimer {
        private BlockingQueue<MyTask> queue = new PriorityBlockingQueue<>();

        private Object locker = new Object();

       public MyTimer() {
            // 创建一个扫描线程.
            Thread t = new Thread(() -> {
                while (true) {
                    try {
                        synchronized (locker) {
                            // 取出队首元素
                            MyTask task = queue.take();
                            // 假设当前时间是 2:30, 任务设定的时间是 2:30, 显然就要执行任务了.
                            // 假设当前时间是 2:30, 任务设定的时间是 2:29, 也是到点了, 也要执行任务.
                            long curTime = System.currentTimeMillis();
                            if (curTime >= task.getTime()) {
                                // 到点了, 改执行任务了!!
                                task.getRunnable().run();
                            } else {
                                // 还没到点
                                queue.put(task);
                                // 没到点, 就等待
                                locker.wait(task.getTime() - curTime);
                            }
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            t.start();
        }
*/
    public void schedule(Runnable runnable,long after) throws InterruptedException {
        MyTask myTask = new MyTask(runnable,after);
        //将创建好的任务加入到队列里
        queue.put(myTask);
        synchronized (locker){
            //这里会唤醒取队首线程里的wait
            locker.notify();
        }
    }

}
public class MyT {
    public static void main(String[] args) throws InterruptedException {
        MyTimer myTimer = new MyTimer();
        //schedule创建一个线程有两个参数:1.线程要执行的任务 2.执行时间:什么时候执行
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到1");
            }
        },3000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到2");
            }
        },4000);
        myTimer.schedule(new Runnable() {
            @Override
            public void run() {
                System.out.println("时间到3");
            }
        },5000);



        System.out.println("开始计时");
    }
}